package karaoke.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import junit.framework.Assert;

import karaoke.excepcion.DAOExcepcion;
import karaoke.modelo.Cliente;
import karaoke.modelo.Comentario;
import karaoke.modelo.JsonResult;
import karaoke.modelo.Local;
import karaoke.modelo.Sala;
import karaoke.negocio.GestionComentarios;
import karaoke.negocio.GestionClientes;
import karaoke.negocio.GestionSalas;

/**
 * Servlet implementation class RecuperaPostServlet
 */
@WebServlet("/ComentarioRegistrarServlet")
public class ComentarioRegistrarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ComentarioRegistrarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	   
       

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		Gson gson = new Gson();
		JsonResult result = new JsonResult();
		String message = "";
		int tipoMsg = JsonResult.WARNING;

	  
	    String comentario = request.getParameter("Comentario");
	    String fechaVisita = request.getParameter("FechaVisita");
	    int calificacion = Integer.parseInt(request.getParameter("Calificacion"));
	 
	    int idSala = Integer.parseInt(request.getParameter("idSala"));
	    int idCliente = Integer.parseInt(request.getParameter("idCliente"));
	    boolean eLog=false;
        GestionComentarios negocio = new GestionComentarios();
        GestionSalas negocioSala = new GestionSalas();
        GestionClientes negocioCliente= new GestionClientes();
        Comentario c=null;
        
		try {
		
			Sala sala=negocioSala.obtener(idSala);
			Cliente cliente=negocioCliente.obtenerPorID(idCliente);

			 c = negocio.insertar(calificacion, comentario, Date.valueOf(fechaVisita), sala, cliente);
			    if (c.getIdcomentario()>0) {
						tipoMsg = JsonResult.SUCCESS;
						message = "["+c.getIdcomentario()+"] Comentario se grabo satisfactoriamente";
					} else {
						tipoMsg = JsonResult.ERROR;
						message = "No se pudo registrar comentario";
				}
					
		
		} catch (DAOExcepcion e) {
			tipoMsg = JsonResult.ERROR;
			message = e.getMessage();
		}
		
		// Guardando datos en el scope REQUEST
	 	result.addMenssage(tipoMsg, message);
		
	    PrintWriter out = response.getWriter();
		out.println(gson.toJson(result));       
			   

       
	
   } //DoPost
     

	
}
